/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;

import java.util.Arrays;

import org.sss.util.BytesComparator;

/**
 * A Packable class that encapsulates a byte[] object.
 */
public class DCPackableBytes implements SelfPackable {
  private static final byte[] EMPTY_BYTES = {};
  private byte hashCode;
  private byte slot;
  private byte[] bytes;
  /**
   * Initialize a newly created DCPackableBytes object so that it represents an
   * empty byte sequence.
   */
  public DCPackableBytes() { this(EMPTY_BYTES,  0, 0); }
  /**
   * Create a DCPackableBytes using the byte array as the initial value.
   * @param bytes this array becomes the backing storage for the object.
   */
  public DCPackableBytes(byte[] bytes, int hashCode, int slot) {
    if (bytes == null) {
      this.bytes = EMPTY_BYTES;
    }
    else {
      this.bytes = bytes;
    }
    this.hashCode = (byte) hashCode;
    this.slot = (byte) slot;
  }
  /**
   * Returns the byte array held in this object.
   * @return the byte array held in this object.
   */
  public byte[] get() { return bytes; }
  /**
   * Indicates whether some other object is "equal to" this one.
   * see {@link Packable#equals}
   */
  @Override
  public boolean equals(Object o) {
    if (o instanceof DCPackableBytes) {
      if (o == this) {
        return true;
      }
      DCPackableBytes o_ = (DCPackableBytes) o;
      byte[] other = ((DCPackableBytes) o).get();
      return BytesComparator.compare(bytes, 0, bytes.length, other, 0, other.length) == 0 &&
        hashCode == o_.hashCode() && slot == o_.slot();
    }
    else {
      return false;
    }
  }
  /**
   * Returns a hash code value for the object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return hashCode;
  }
  /**
   * Indicates which slot this object should be put.
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return slot;
  }
  /**
   * Deserialize this object from the specified raw byte array.
   * see {@link SelfPackable#loadBytes}
   */
  @Override
  public DCPackableBytes loadBytes(byte[] bytes) throws Exception {
    this.hashCode = bytes[0];
    this.slot = bytes[1];
    this.bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
// for StreamDB
//   DCPackableBytes won't work as expected, since dataserver has no system key
//     this.hashCode = bytes[0];   // oh, what a quick and dirty hack...
//     this.slot = bytes[0];
//     this.bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
    return this;
  }
  /**
   * Serialize this object into a sequence of bytes.
   * see {@link SelfPackable#toBytes}
   */
  @Override
  public byte[] toBytes() throws Exception {
    byte[] tmp = new byte[2 + bytes.length];
    tmp[0] = hashCode;
    tmp[1] = slot;
    for (int i=0; i<bytes.length; i++) {
      tmp[2 + i] = bytes[i];
    }
    return tmp;
  }

  @Override
  public long getRoughSize() {
    return 16 + 2 + bytes.length;
  }
}
